<?php

namespace app\admin\controller;

class VideoList extends Base
{
    public function all()
    {
        $videoTitle = null;
        $where = [];
        if (input('?id')) {
            $videoTitle = model('Video')->where('id', input('id'))->value('title');
        }
        $videoLists = model('VideoList')->where($where)->with('video')->order(['video_id' => 'desc', 'id' => 'asc'])->paginate(10);
        $viewData = [
            'videoTitle' => $videoTitle,
            'videoLists' => $videoLists
        ];
        $this->assign($viewData);
        return view();
    }

    public function add()
    {
        if (request()->isAjax()) {
            $data = [
                'title' => input('post.title'),
                'video_id' => input('post.video_id'),
                'url' => input('post.url')
            ];
            $result = model('VideoList')->add($data);
            if ($result == 1) {
                $this->success('操作成功！', 'admin/videoList/all');
            }else {
                $this->error($result);
            }
        }
        $videos = model('Video')->select();
        $viewData = [
            'videos' => $videos
        ];
        $this->assign($viewData);
        return view();
    }

    public function edit()
    {
        if (request()->isAjax()) {
            $data = [
                'id' => input('post.id'),
                'title' => input('post.title'),
                'video_id' => input('post.video_id'),
                'url' => input('post.url')
            ];
            $result = model('VideoList')->edit($data);
            if ($result == 1) {
                $this->success('操作成功！', 'admin/videoList/all');
            }else {
                $this->error($result);
            }
        }
        $videos = model('Video')->select();
        $videoListInfo = model('VideoList')->find(input('id'));
        $viewData = [
            'videos' => $videos,
            'videoListInfo' => $videoListInfo
        ];
        $this->assign($viewData);
        return view();
    }

    public function del()
    {
        $videoListInfo = model('VideoList')->find(input('post.id'));
        $result = $videoListInfo->delete();
        if ($result) {
            $this->success('操作成功！', 'admin/videoList/all');
        }else {
            $this->error('操作失败！');
        }
    }
}
